package yaty.lee.tencent.weibo.modules.oauth;

import java.awt.Desktop;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import yaty.lee.tencent.weibo.modules.oauth1.vo.AccessTokenVO;
import yaty.lee.tencent.weibo.modules.oauth1.vo.RequestTokenVO;
import yaty.lee.tencent.weibo.modules.oauth2.ig.vo.ImplicitAuthVO;
import yaty.lee.tencent.weibo.util.HttpURLConnectionUtil;
import yaty.lee.tencent.weibo.util.OAuthPropertiesUtil;
import yaty.lee.tencent.weibo.util.ReflectUtil;

public class GrantHelper {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void oauth1_1() throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
        RequestTokenVO vo = new RequestTokenVO();
        vo.setOauth_callback("null");
        vo.setOauth_consumer_key(OAuthPropertiesUtil.getKey());
        vo.setOauth_nonce(UUID.randomUUID().toString().replaceAll("-", ""));
        vo.setOauth_timestamp(System.currentTimeMillis() / 1000 + "");

        List<String> strs = new ArrayList<String>();
        strs.add("oauth_callback=" + vo.getOauth_callback());
        strs.add("oauth_consumer_key=" + OAuthPropertiesUtil.getKey());
        strs.add("oauth_nonce=" + vo.getOauth_nonce());
        strs.add("oauth_signature_method=" + vo.getOauth_signature_method());
        strs.add("oauth_timestamp=" + vo.getOauth_timestamp());
        strs.add("oauth_version=" + vo.getOauth_version());

        String basestring = "GET&" + URLEncoder.encode(vo.getReqUrl(), "UTF-8") + "&" + URLEncoder.encode(ReflectUtil.join(strs, "&"), "UTF-8");
        Mac mac = Mac.getInstance("HmacSHA1");
        SecretKeySpec secret = new SecretKeySpec((OAuthPropertiesUtil.getSecret() + "&").getBytes(), mac.getAlgorithm());
        mac.init(secret);
        byte[] result = Base64.encodeBase64(mac.doFinal(basestring.getBytes()));
        vo.setOauth_signature(new String(result));

        Map<String, String> map = HttpURLConnectionUtil.urlParamAnalysis(HttpURLConnectionUtil.get(vo));
        System.out.println(map.get("oauth_token"));
        System.out.println(map.get("oauth_token_secret"));

        String url = "http://open.t.qq.com/cgi-bin/authorize?oauth_token=" + map.get("oauth_token");
        System.out.println(url);

        // fb180e44e76743bd970c4938d8f610b5
        // ffeddce7dc1628738ec0cc42bb894106
        // http://open.t.qq.com/cgi-bin/authorize?oauth_token=fb180e44e76743bd970c4938d8f610b5

        // http://open.t.qq.com/cgi-bin/authorize?oauth_token=fb180e44e76743bd970c4938d8f610b5&vcode=537635&openid=8AC53B54E902609AED4AC620A4B6E2F9&openkey=D3973B9A98BCB263DA85BD140A88FED3&checkType=verifycode
    }

    @Test
    public void oauth1_2() throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
        AccessTokenVO accessTokenVO = new AccessTokenVO();
        accessTokenVO.setOauth_consumer_key(OAuthPropertiesUtil.getKey());
        accessTokenVO.setOauth_nonce(UUID.randomUUID().toString().replaceAll("-", ""));
        accessTokenVO.setOauth_timestamp(System.currentTimeMillis() / 1000 + "");
        accessTokenVO.setOauth_token("fb180e44e76743bd970c4938d8f610b5");
        accessTokenVO.setOauth_verifier("537635");

        List<String> strs = new ArrayList<String>();
        strs.add("oauth_consumer_key=" + OAuthPropertiesUtil.getKey());
        strs.add("oauth_nonce=" + accessTokenVO.getOauth_nonce());
        strs.add("oauth_signature_method=" + accessTokenVO.getOauth_signature_method());
        strs.add("oauth_timestamp=" + accessTokenVO.getOauth_timestamp());
        strs.add("oauth_token=" + accessTokenVO.getOauth_token());
        strs.add("oauth_verifier=" + accessTokenVO.getOauth_verifier());
        strs.add("oauth_version=" + accessTokenVO.getOauth_version());

        String basestring = "GET&" + URLEncoder.encode(accessTokenVO.getReqUrl(), "UTF-8") + "&" + URLEncoder.encode(ReflectUtil.join(strs, "&"), "UTF-8");
        Mac mac = Mac.getInstance("HmacSHA1");
        SecretKeySpec secret = new SecretKeySpec((OAuthPropertiesUtil.getSecret() + "&" + "ffeddce7dc1628738ec0cc42bb894106").getBytes(), mac.getAlgorithm());
        mac.init(secret);
        byte[] result = Base64.encodeBase64(mac.doFinal(basestring.getBytes()));
        accessTokenVO.setOauth_signature(new String(result));

        System.out.println(HttpURLConnectionUtil.get(accessTokenVO));

        // oauth_token=bbdc7ec6fafb4bca9879cc05ea0906b8&oauth_token_secret=f847c30719c58dd29e0f5cc155d683b5&name=po__oq-
        // oauth_token=bbdc7ec6fafb4bca9879cc05ea0906b8&oauth_token_secret=f847c30719c58dd29e0f5cc155d683b5&name=po__oq-
    }

    @Test
    public void getImplicitGrantUrl() throws IOException, URISyntaxException {
        ImplicitAuthVO implicitAuthVO = new ImplicitAuthVO();
        String url = implicitAuthVO.getReqUrl() + "?" + ReflectUtil.getKeyValuePairs(implicitAuthVO);
        System.out.println(url);
        if (Desktop.isDesktopSupported()) {
            Desktop.getDesktop().browse(new URI(url));
        }
    }

}
